package com.weblog.article.dao;

import com.weblog.model.article.Article;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.NoRepositoryBean;

import java.util.List;
import java.util.Map;

/**
 * article数据访问接口
 * @author Administrator
 *
 */
public interface ArticleDao extends JpaRepository<Article,String>, JpaSpecificationExecutor<Article> {


    /**
     * 修改状态(删除或审核)
     * @param state 目标状态
     * @param aid 文章id
     */
    @Query(value = "update tb_article set state=?1 where aid=?2",nativeQuery = true)
    @Modifying
    void updateStateById(String state, String aid);

    /**
     * 点赞
     * @param aid 文章id
     */
    @Query(value = "update tb_article set thumbup=thumbup+1 where aid =?1",nativeQuery = true)
    @Modifying
    void thumbup(String aid);

    /**
     * 增加文章评论数
     * @param aid 文章id
     */
    @Query(value = "update tb_article set commentnum=commentnum+1 where aid=?1",nativeQuery = true)
    @Modifying
    void incrArticleCommentNum(String aid);

    /**
     * 减少文章评论数
     * @param aid 文章id
     */
    @Query(value = "update tb_article set commentnum=commentnum+?2 where aid=?1",nativeQuery = true)
    @Modifying
    void decrArticleCommentNum(String aid,int size);


    /**
     * 根据id查询标题
     * @param aid 文章id
     * @return 文章标题
     */
    @Query(value = "select title from tb_article where aid=?1  ",nativeQuery = true)
    String selectTitle(String aid);


    /**
     * 根据标题、用户和文章id查询
     * @param title 标题
     * @param userid 用户id
     * @param aids 文章id集合
     * @return
     */
    @Query(value = "SELECT a.aid as aid,a.title as title FROM Article a WHERE  LOCATE(?1,a.title)>0 and a.userid=?2 and a.aid not in ?3")
    List<Map<String,String>> findAidByTitleAndUseridAndAids(String title, String userid,List<String>aids);

}
